const app = getApp()

Page({
    data: {
    },
    onLoad:function(params){
        var me = this;
        var redirectUrl = params.redirectUrl;
        if(redirectUrl != null && redirectUrl != undefined && redirectUrl != ''){
            //替换 
            // /#表示替换井号，/g表示替换全局的
           redirectUrl = redirectUrl.replace(/#/g,"?");
           redirectUrl = redirectUrl.replace(/@/g,"=");
        }
    },

    doLogin: function(e){
        var me = this;
        var formObject = e.detail.value;
        var username = formObject.username;
        var password = formObject.password;
        //简单验证
        if(username.length == 0 || password.length == 0){
            wx.showToast({
                title: '用户名或密码不能为空！',
                icon: "none",
                duration: 3000
            })
        }else{
            var serverUrl = app.serverUrl;
            wx.showLoading({
              title: '少女祈祷中...',
            }),
            wx.request({
                url: serverUrl + '/login.do' ,
                method: 'POST',
                data:{
                    username: username,
                    password: password
                },
                header:{
                    'content-type': 'application/json'
                },
                success: function(res){
                    wx.hideLoading();
                    console.log(res.data);
                    if(res.data.status == 200){
                        wx.showToast({
                          title: res.data.msg,
                          icon: "success",
                          duration: 3000
                        })
                        // app.userInfo = res.data.data;
                        //fixme 修改原有的全局对象为本地缓存
                        app.setGlobalUserInfo(res.data.data);
                        //页面跳转
                        var redirectUrl = me.redirectUrl;
                        if(redirectUrl != null && redirectUrl != undefined && redirectUrl != ''){
                            wx.redirectTo({
                              url: redirectUrl,
                            })
                        }else{
                            wx.redirectTo({
                              url: '../mine/mine',
                            })
                        }
                    }
                    else{
                        wx.showToast({
                            title: res.data.msg,
                            icon: "none",
                            duration: 3000
                          })
                    }
                }

            })
        }
    },
    goRegistPage:function(e){
        wx.navigateTo({
          url: '../userRegist/regist',
        })
    }
})